﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreClientModule;

namespace MBBStoreClient
{
    public partial class frmUserManager : Form
    {
        UserManager um = new UserManager();
        UserType[] eUT = new UserType[2];
        bool isAdding = false;
        public frmUserManager()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            um.ProcessCompleted += new ModuleEventsHandler(um_ProcessCompleted);
            um.ProcessError += new ModuleEventsHandler(um_ProcessError);
            btnAdd.Click += new EventHandler(btnAdd_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnRemove.Click += new EventHandler(btnRemove_Click);
            lstUsers.DoubleClick += new EventHandler(lstUsers_DoubleClick);
        }

        void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstUsers.SelectedIndex > -1)
            {
                DialogResult dlg = MessageBox.Show("This will permanently remove the selected user. Are you sure?",
                    "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    um.Delete(lstUsers.SelectedItem.ToString());
                }
                
            }
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text.Equals("") || txtPassword.Text.Equals(""))
            {
                MessageBox.Show("Please complete the required fields.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (isAdding)
            {
                UserType ut = new UserType();
                ut.Username = txtUsername.Text;
                ut.Password = txtPassword.Text;
                um.Add(ut);
            }
            else
            {
                eUT[0].Username = txtUsername.Text;
                eUT[0].Password = txtPassword.Text;
                um.Update(eUT);
            }
            
        }

        void lstUsers_DoubleClick(object sender, EventArgs e)
        {
            if (lstUsers.SelectedIndex > -1)
            {
                lstUsers.Enabled = false;
                btnAdd.Enabled = false;
                btnRemove.Enabled = false;
                um.GetUser(lstUsers.SelectedItem.ToString());
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            isAdding = false;
            grpDetails.Enabled = false;
            lstUsers.Enabled = true;
            btnRemove.Enabled = true;
            btnAdd.Enabled = true;
            txtUsername.Text = "";
            txtPassword.Text = "";
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            isAdding = true;
            grpDetails.Enabled = true;
            lstUsers.Enabled = false;
            btnAdd.Enabled = false;
            btnRemove.Enabled = false;
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtUsername.Focus();
        }

        void um_ProcessError(int procCode, string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg);
            });
        }
        void um_ProcessCompleted(int procCode, string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                if (procCode == 22)
                {
                    lstUsers.Items.Clear();
                    for (int i = 0; i < um.Users.Length; i++)
                    {
                        lstUsers.Items.Add(um.Users[i]);
                    }
                }
                if (procCode == 26)
                {
                    grpDetails.Enabled = true;
                    eUT[1].Username = um.User.Username;
                    eUT[1].Password = um.User.Password;
                    txtUsername.Text = um.User.Username;
                    txtPassword.Text = um.User.Password;
                    um.GetUserList();
                }
                if (procCode == 23 || procCode == 24 || procCode == 25)
                {
                    isAdding = false;
                    grpDetails.Enabled = false;
                    lstUsers.Enabled = true;
                    btnRemove.Enabled = true;
                    btnAdd.Enabled = true;
                    txtUsername.Text = "";
                    txtPassword.Text = "";
                    um.GetUserList();
                }
            });
        }







        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
            um.GetUserList();
        }
    }
}
